//
//  TTDepartureCell.m
//  TrainTrack
//
//  Created by Jonathan Bunde-Pedersen on 04/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TTDepartureCell.h"


@implementation TTDepartureCell

static UIImage *lineA = nil;
static UIImage *lineB = nil;
static UIImage *lineBx = nil;
static UIImage *lineC = nil;
static UIImage *lineE = nil;
static UIImage *lineF = nil;
static UIImage *lineH = nil;

@synthesize lineIcon, lineDirection, eta, etaUnitLabel, time; 

+ (void) initializeGraphics {
	lineA = [[UIImage imageNamed:@"A.png"] retain];
	lineB = [[UIImage imageNamed:@"B.png"] retain];
	lineBx = [[UIImage imageNamed:@"Bx.png"] retain];
	lineC = [[UIImage imageNamed:@"C.png"] retain];
	lineE = [[UIImage imageNamed:@"E.png"] retain];
	lineF = [[UIImage imageNamed:@"F.png"] retain];
	lineH = [[UIImage imageNamed:@"H.png"] retain];
}

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
        // Initialization code
	    
	    self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
	    
	    self.lineIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,48,48)];
	    
	    self.lineDirection = [[UILabel alloc] initWithFrame:CGRectMake(0,0,48,48)];
	    self.lineDirection.backgroundColor = [UIColor clearColor];
	    self.lineDirection.opaque = YES;
	    self.lineDirection.textColor = [UIColor blackColor];
	    self.lineDirection.font = [UIFont systemFontOfSize:20];;
	    self.lineDirection.userInteractionEnabled = NO;
	    
	    self.time = [[UILabel alloc] initWithFrame:CGRectMake(0,0,48,48)];
	    self.time.backgroundColor = [UIColor clearColor];
	    self.time.opaque = YES;
	    self.time.textColor = [UIColor blackColor];
	    self.time.font = [UIFont systemFontOfSize:18];;
	    self.time.userInteractionEnabled = NO;
	    
	    self.eta = [[UILabel alloc] initWithFrame:CGRectMake(0,0,48,48)];
	    self.eta.backgroundColor = [UIColor clearColor];
	    self.eta.opaque = YES;
	    self.eta.textColor = [UIColor blackColor];
	    self.eta.font = [UIFont boldSystemFontOfSize:30];;
	    self.eta.userInteractionEnabled = NO;
	    self.eta.textAlignment = UITextAlignmentCenter;
	    
	    self.etaUnitLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,48,48)];
	    self.etaUnitLabel.backgroundColor = [UIColor clearColor];
	    self.etaUnitLabel.opaque = YES;
	    self.etaUnitLabel.textColor = [UIColor blackColor];
	    self.etaUnitLabel.font = [UIFont boldSystemFontOfSize:10];;
	    self.etaUnitLabel.userInteractionEnabled = NO;
	    self.etaUnitLabel.textAlignment = UITextAlignmentCenter;
	    
	    [self addSubview:self.lineIcon];
	    [self addSubview:self.lineDirection];
	    [self addSubview:self.time];
	    [self addSubview:self.eta];
	    [self addSubview:self.etaUnitLabel];
    }
    return self;
}

- (Departure *)departure
{
	return departure;
}


- (void)setDeparture:(Departure *)theDeparture
{
	departure = theDeparture;
	
	if([departure.line isEqualToString:@"A"])
		lineIcon.image = lineA;
	else if([departure.line isEqualToString:@"B"])
		lineIcon.image = lineB;
	else if([departure.line isEqualToString:@"Bx"])
		lineIcon.image = lineBx;
	else if([departure.line isEqualToString:@"C"])
		lineIcon.image = lineC;
	else if([departure.line isEqualToString:@"E"])
		lineIcon.image = lineE;
	else if([departure.line isEqualToString:@"F"])
		lineIcon.image = lineF;
	else if([departure.line isEqualToString:@"H"])
		lineIcon.image = lineH;
	
	self.lineDirection.text = departure.direction;
	
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"d"];
	NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
	int day = [dateString intValue];
	
	[dateFormatter setDateFormat:@"Y"];
	dateString = [dateFormatter stringFromDate:[NSDate date]];
	int year = [dateString intValue];
	
	[dateFormatter setDateFormat:@"M"];
	dateString = [dateFormatter stringFromDate:[NSDate date]];
	int month = [dateString intValue];
	
	
	
	int hour = (departure.valid_at_time/100);
//	if (hour < 3) {
//		hour += 24;
//	}
	int minute = (departure.valid_at_time%100);
	
	int dst_offset = ([[NSTimeZone localTimeZone] isDaylightSavingTime]) ? 1 : 0;
	
	NSDate *d = [[NSDate alloc] initWithString:[[NSString alloc] initWithFormat: @"%d-%d-%d %d:%d:%d +0%d00", year, month, day, hour, minute, 0, 1 + dst_offset]];
	
	[dateFormatter setDateFormat:@"k:mm"];
	NSString *stime = [dateFormatter stringFromDate:d];
	
	self.time.text = stime;
	
	int ti = (((int)[d timeIntervalSinceNow]) / 60);
	if (ti<0) ti = (((int)[[d addTimeInterval:86400] timeIntervalSinceNow]) / 60);
	NSString *label = @"min";
	UIColor *labelColor = [UIColor blackColor];
	if (ti > 59) { ti /= 60; label = @"hr"; labelColor = [UIColor grayColor]; }
	
	self.eta.text = [NSString stringWithFormat:@"%d", ti];
	self.etaUnitLabel.text = label;
	self.etaUnitLabel.textColor = labelColor;
	self.eta.textColor = labelColor;
	self.time.textColor = labelColor;
	self.lineDirection.textColor = labelColor;
	
	[dateFormatter release];
}

- (void)layoutSubviews {
	
#define RIGHT_PADDING 8
	
#define ICON_COLUMN_OFFSET 10
#define ICON_COLUMN_WIDTH 40
	
#define DIRECTION_COLUMN_OFFSET 60
#define DIRECTION_COLUMN_WIDTH 160
	
#define TIME_COLUMN_OFFSET 210
#define TIME_COLUMN_WIDTH 50
	
#define ETA_COLUMN_OFFSET 265
#define ETA_COLUMN_WIDTH 40
	
#define UPPER_ROW_TOP 8
#define MIDDLE_ROW_TOP 8
#define LOWER_ROW_TOP 8
	
	[super layoutSubviews];
	
	CGRect frame;
	
	//self.frame = CGRectMake(0, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
	
	// place the line icon
	frame = CGRectMake(ICON_COLUMN_OFFSET, UPPER_ROW_TOP, ICON_COLUMN_WIDTH, 40);
	self.lineIcon.frame = frame;
	
	// place the direction label
	frame = CGRectMake(DIRECTION_COLUMN_OFFSET, UPPER_ROW_TOP, DIRECTION_COLUMN_WIDTH, 40);
	self.lineDirection.frame = frame;
	
	// place the time label
	frame = CGRectMake(TIME_COLUMN_OFFSET, UPPER_ROW_TOP, TIME_COLUMN_WIDTH, 40);
	self.time.frame = frame;
	
	// place the eta label
	frame = CGRectMake(ETA_COLUMN_OFFSET, UPPER_ROW_TOP, ETA_COLUMN_WIDTH, 30);
	self.eta.frame = frame;
	
	// place the eta unit label
	frame = CGRectMake(ETA_COLUMN_OFFSET, self.eta.frame.origin.y+self.eta.frame.size.height-5, ETA_COLUMN_WIDTH, 20);
	self.etaUnitLabel.frame = frame;		
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
    [super dealloc];
}


@end
